package com.chis.jxc;

import com.chis.jm.bean.JxcSynBean;
import com.chis.jm.bean.jxc.CntstmInfoBean;
import com.chis.jm.bean.jxc.TdJxcCntstmInfoBean;
import com.chis.jmdatakafka.service.IKafkaMsgService;
import com.chis.jmdatatimer.comm.JedisConnect;
import com.chis.jmdatatimer.comm.JedisSerializeUtil;
import com.chis.jmdatatimer.comm.TxMgmtModule;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Description  手工同步客户端门诊出库，以前门诊出库时走的spark同步
 * @Company zwx
 * @author wlj
 * @version 2019年05月09日
 */
public class TestSynJxc {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring.xml","consumer_sparkdeal.xml"});
        context.start();
        TxMgmtModule txMgmtModule = (TxMgmtModule) context.getBean("TxMgmtModule");
        List<CntstmInfoBean> list = txMgmtModule.findkc();
        Map<Long,List<CntstmInfoBean>> unitmap=new HashMap<>();
        for(CntstmInfoBean cntstmInfoBean : list){
            List<CntstmInfoBean> list1 = new ArrayList<>();
            if(unitmap.containsKey(cntstmInfoBean.getUnitCode())){
                list1 = unitmap.get(cntstmInfoBean.getUnitCode());
            }
            list1.add(cntstmInfoBean);
            unitmap.put(cntstmInfoBean.getUnitCode(),list1);
        }
        ShardedJedisPool jedisPool = JedisConnect.getInstance().jedisPoolJxc;
        ShardedJedis jedis = jedisPool.getResource();
        jedis.select(1);
        IKafkaMsgService kafkaservice = (IKafkaMsgService) context.getBean("kafkaMsgService");
        if(kafkaservice == null){
            throw new RuntimeException("获取kafka服务失败");
        }
        Map<String,String> manmap = findman();
        for(Long unitcode : unitmap.keySet()){
            //System.out.println("====================================");
            List<CntstmInfoBean> list2 = unitmap.get(unitcode);
            Map<String,CntstmInfoBean> map1 = new HashMap<>();
            for(CntstmInfoBean t : list2){
                map1.put(t.getGuid(),t);
            }
            List<CntstmInfoBean> list3 = (List<CntstmInfoBean>) JedisSerializeUtil.unserialize(jedis.get(unitcode.toString().getBytes()));
            Map<String,CntstmInfoBean> map2 = new HashMap<>();
            for(CntstmInfoBean t : list3){
                map2.put(t.getGuid(),t);
            }
            List<String[]>  kcList = new ArrayList<>();
            for(String guid : map1.keySet()){
                CntstmInfoBean mysql = map1.get(guid);
                CntstmInfoBean redis = map2.get(guid);
                if(mysql != null && redis != null && !mysql.getStmCnt().equals(redis.getStmCnt())){
                    //System.out.println("#####################");
                    //System.out.println(unitcode);
                    //System.out.println(guid);
                    //System.out.println(mysql.getStmCnt()-redis.getStmCnt());
                    kcList.add(new String[]{guid,String.valueOf(mysql.getStmCnt()-redis.getStmCnt())});
                }
            }
            System.out.println("=====================================");
            if(kcList.size() > 0){
                JxcSynBean jxcSynBean = new JxcSynBean();
                jxcSynBean.setUnitCode(unitcode);
                jxcSynBean.setKcList(kcList);
                jxcSynBean.setCkTime(new Date());
                jxcSynBean.setManCode(manmap.containsKey(String.valueOf(unitcode))?Long.valueOf(manmap.get(String.valueOf(unitcode))):Long.valueOf(unitcode+"998"));
                System.out.println("单位编码：" + jxcSynBean.getUnitCode());
                System.out.println("出库时间：" + jxcSynBean.getCkTime());
                System.out.println("出库人员：" + jxcSynBean.getManCode());
                System.out.println("出库信息：" + conbvert(jxcSynBean.getKcList()));
                System.out.println("出库人份：" + conbvert(jxcSynBean.getPsnList()));
                //kafkaservice.produceRedisMsg("TOPIC_JXC_SYN",jxcSynBean);
            }
        }
    }

    public static Map<String,String> findman(){
        Map<String,String> map =new HashMap<>();
        String s = "3202821501,3202821501016#3202050501,3202050501042#3202140301,3202140301009#3201250401,3201250401018#3201040601,3201040601022#3201061301,3201061301004#3204110601,3204110601002#3202050401,3202050401068#3204020102,3204020102028#3202051201,3202051201011#3201060301,3201060301007#3202811901,3202811901006#3201050202,3201050202038#3201130201,3201130201036#3201061401,3201061401007#3201130401,3201130401011#3201130501,3201130501035#3202140401,3202140401002#3202050302,3202050302046#3204041001,3204041001020#3201050603,3201050603041#3201120701,3201120701011#3204040601,3204040601019#3204131001,3204131001041#3201130703,3201130703019#3202060102,3202060102036#3204040803,3204040803002#3201131001,3201131001034#3204122301,3204122301029#3201021001,3201021001006#3204133101,3204133101064#3201150501,3201150501094#3201240303,3201240301026#3201120604,3201120604019#3201250101,3201250101021#3201150401,3201150401049#3202140702,3202140702005#3202111901,3202111901001#3204110402,3204110402006#3204131701,3204131701035#3201061601,3201061601007#3201140401,3201140401024#3202810801,3202810801016#3201041202,3201041202012#3201020202,3201020202018#3201150101,3201150101062#3201061101,3201061101007";
        String[] strings  = s.split("#");
        for(String s1 : strings){
           String[] strings1 =  s1.split(",");
           map.put(strings1[0],strings1[1]);
        }
        return map;
    }

    public static String conbvert(List<String[]> list) {
        if (list != null) {
            StringBuilder sb = new StringBuilder();
            for (String[] str : list) {
                StringBuilder sb2 = new StringBuilder();
                for (String s : str) {
                    sb2.append("[").append(s).append("]");
                }
                sb.append("{").append(sb2).append("}");
            }
            return sb.toString();
        } else {
            return "";
        }
    }
}
